package com.common.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.common.pager.PageHelper;
import com.common.util.BeanUtils;

public class DB4ObjUtil extends DBBaseUtil {
	
	private static Logger log = Logger.getLogger(DB4ObjUtil.class);
	
	/**
	 * 分页查询并封装对象
	 * @param obj
	 * @param sql
	 * @param pager
	 * @param objects
	 * @return
	 * @throws NumberFormatException
	 * @throws Exception
	 */
	public static Vector executePager4Obj(Object obj,String sql, PageHelper pager,Object...objects)  {
		Connection conn = DBSource.getConnection();
		Vector ver = new Vector();
		try {
			pager.setTotalCount(Integer.parseInt(String.valueOf(getObjectSingleByConn(conn,SqlUtil.getAnalyseSQLOfcount(sql),objects))));
			
			boolean hasOffset = pager.getCurrentPage()>1?true:false;
			sql = SqlUtil.getSqlLimit(sql, hasOffset, (pager.getCurrentPage() - 1) * pager.getRowPage(), pager.getRowPage());
			log.info("===pagersql==="+sql);	
			
			ver = execute4ObjByConn(conn,obj,sql,objects);
		
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		} finally {
			closeConnection(conn, null, null);
		}
		return ver;
	}
	
	/**
	 * 查询数据前多少条数据
	 * @param obj
	 * @param sql
	 * @param objects
	 * @return
	 */
	public static Vector executeTop4Obj(Object obj,String sql,int rows,Object...objects) {
		sql = SqlUtil.getSqlLimit(sql, false,0, rows);
		return execute4Obj(obj,sql,objects);
	}
	
	/**
	 * 查询数据
	 * @param obj
	 * @param sql
	 * @param objects
	 * @return
	 */
	public static Vector execute4Obj(Object obj,String sql,Object...objects) {
		Connection conn = DBSource.getConnection();
		Vector ver = new Vector();
		try {
		   ver = execute4ObjByConn(conn,obj,sql,objects);
		
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		} finally {
			closeConnection(conn, null, null);
		}
		return ver;
	}
	
	public static Vector execute4ObjByConn(Connection conn,Object obj,String sql,Object...objects){
		
		
		Vector ver = new Vector();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		ResultSetMetaData rmd = null;
		try {
			stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
			for(int i=0;i<objects.length;i++){
				int n=i+1;
				setStatemnt(stmt, n, objects[i]);
			}
			rs = stmt.executeQuery();
			rmd = rs.getMetaData();
			int columCount = rmd.getColumnCount();
			int i = 0;
			while (rs.next() ) {
				i = i + 1;
				Object o = new Object();
				Object newObj = obj.getClass().newInstance();
				for (int j = 0; j < columCount; j++) {
					String colunmName = rmd.getColumnName(j + 1);
					if(colunmName.toLowerCase().indexOf("rownum_")==-1){
					o = rs.getObject(colunmName);
					setProperty4Table(newObj, o, colunmName);
					}
				}
				ver.addElement(newObj);
			}
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		} finally {
			closeConnection(null, stmt, rs);
		}
		return ver;
	}
	

	
	

	
	private static void setProperty(Object newObj,Object columObj,String propertyName) throws NoSuchFieldException{
		if(columObj!=null){
			
			BeanUtils.forceSetProperty(newObj, propertyName,columObj);
		}
	}
	private static void setProperty4Table(Object newObj,Object columObj,String propertyName) throws NoSuchFieldException{
		if(columObj!=null){
			BeanUtils.forceSetProperty(newObj, SqlUtil.db2ObjStr(propertyName),columObj);
		}
	}
	
	
}
